package org.exhaust.framework.net.common.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

public abstract class BaseFrame {
    public byte[] encode() {
        ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.directBuffer();
        try {
            this.encodeData(byteBuf);
            byte[] data = new byte[byteBuf.readableBytes()];
            byteBuf.getBytes(byteBuf.readerIndex(), data);
            return data;
        } finally {
            byteBuf.release();
        }
    }


    public void encode(ByteBuf out) {
        this.encodeData(out);
    }

    protected abstract void encodeData(ByteBuf out);

    public abstract void decode(ByteBuf in, FrameHeader header);
}
